/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.manufacturers.Manufacturer1;
import org.manufacturers.Manufacturer1Service;
import org.manufacturers.Manufacturer2;
import org.manufacturers.Manufacturer2Service;
import org.manufacturers.Manufacturer3;
import org.manufacturers.Manufacturer3Service;
import org.manufacturers.PurchaseOrder;
import org.warehouse1.Item;
import org.warehouse1.Warehouse1WS;
import org.warehouse1.Warehouse1WSService;
import org.warehouse2.Warehouse2WS;
import org.warehouse2.Warehouse2WSService;
import org.warehouse3.Warehouse3WS;
import org.warehouse3.Warehouse3WSService;



public class adminServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {

        String cont = "";
        int reqType = Integer.parseInt( request.getParameter("reqtype") );

        if( reqType==1 )
        {
            // Inventory
            int wh = Integer.parseInt( request.getParameter("warehouse") );
            cont = getInventory(wh);
        }
        else if( reqType==2 )
        {
            // Orders
            int manu = Integer.parseInt( request.getParameter("manufacturer") );
            cont = getOrders(manu);
        }

        //request.setAttribute("INV", inv);
        PrintWriter out = response.getWriter();
        response.setContentType("text/plain");
        out.println(cont);

       //request.getRequestDispatcher("report.jsp").forward(request, response);
       
    }

    private String getInventory(int wh)
    {
        // INVENTORIES
        String cat = "<ul class='f'>";
        
        if(wh == 1)
        {
            Warehouse1WSService whservice1 = new Warehouse1WSService();
            Warehouse1WS whserve1 = whservice1.getWarehouse1WSPort();
            List<org.warehouse1.Item> invList = whserve1.getCatalog();

            for(Item it : invList)
            {
                cat += "<li>" + it.getManufacturerName().substring(0,1).toUpperCase() + it.getManufacturerName().substring(1) + " " + it.getProductType() + "<ul>";
                cat += "<li>Unit Price: " + it.getUnitPrice() + "</li>";
                cat += "<li>Quantity: " + it.getQuantity() + "</li>";
                cat += "</ul></li><br/>";
            }
        }
        if(wh == 2)
        {
            Warehouse2WSService whservice2 = new Warehouse2WSService();
            Warehouse2WS whserve2 = whservice2.getWarehouse2WSPort();
            List<org.warehouse2.Item> invList = whserve2.getCatalog();

            for(org.warehouse2.Item it : invList)
            {

                cat += "<li>" + it.getManufacturerName().substring(0,1).toUpperCase() + it.getManufacturerName().substring(1) + " " + it.getProductType() + "<ul>";
                cat += "<li>Unit Price: " + it.getUnitPrice() + "</li>";
                cat += "<li>Quantity: " + it.getQuantity() + "</li>";
                cat += "</ul></li><br/>";
            }
        }
        if(wh == 3)
        {
                Warehouse3WSService whservice3 = new Warehouse3WSService();
            Warehouse3WS whserve3 = whservice3.getWarehouse3WSPort();
            List<org.warehouse3.Item> invList = whserve3.getCatalog();

             for(org.warehouse3.Item it : invList)
             {
                cat += "<li>" + it.getManufacturerName().substring(0,1).toUpperCase() + it.getManufacturerName().substring(1) + " " + it.getProductType() + "<ul>";
                cat += "<li>Unit Price: " + it.getUnitPrice() + "</li>";
                cat += "<li>Quantity: " + it.getQuantity() + "</li>";
                cat += "</ul></li><br/>";
            }
        }

        cat += "</ul>";

        return cat;
    }
    
     private String getOrders(int manuNum)
    {
        // ORDERS
        List<PurchaseOrder> pOList = null;

        if(manuNum == 1)
        {
            Manufacturer1Service manuSer = new Manufacturer1Service();
            Manufacturer1 manuWS1 = manuSer.getManufacturer1Port();
            pOList = manuWS1.getOrders(true);
        }
        else if (manuNum == 2)
        {
            Manufacturer2Service manuSer2 = new Manufacturer2Service();
            Manufacturer2 manuWS2 = manuSer2.getManufacturer2Port();
            pOList = manuWS2.getOrders(true);
        }
        else if (manuNum == 3)
        {
            Manufacturer3Service manuSer3 = new Manufacturer3Service();
            Manufacturer3 manuWS3 = manuSer3.getManufacturer3Port();
            pOList = manuWS3.getOrders(true);
        }

        String ordersHTML = "<ul class='f'>";

        if( !pOList.isEmpty() )
        {
            for(PurchaseOrder pO : pOList)
            {
                ordersHTML += "<li>Order No. " + pO.getOrderNum() + "<ul>";
                ordersHTML += "<li>Customer Reference: " + pO.getCustomerRef() + "</li>";
                ordersHTML += "<li>Product: " + pO.getProduct().getManufacturerName() + " " + pO.getProduct().getProductType() + "</li>";
                ordersHTML += "<li>Unit Price: " + pO.getUnitPrice() + "</li>";
                ordersHTML += "<li>Quantity: " + pO.getQuantity() + "</li>";
                ordersHTML += "</ul></li><br/>";
            }
        }
        else
        {
            ordersHTML += "No Orders made";
        }

        ordersHTML += "</ul>";

        return ordersHTML;
    }
    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
